package com.itheima.service.company.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.common.utils.UtilFuns;
import com.itheima.dao.system.CompanyDao;
import com.itheima.domain.system.Company;
import com.itheima.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class CompanyServiceImpl implements ICompanyService {


    @Autowired
    private CompanyDao companyDao;

    /**
     * 查询所有用户
     *
     * @return List<Company>
     */
    @Override
    public List<Company> findAll() {
        return companyDao.findAll();
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return Company
     */
    @Override
    public Company findById(String id) {
        return companyDao.findById(id);
    }

    /**
     * 保存用户
     *
     * @param company
     * @return
     */
    @Override
    public boolean save(Company company) {
        //1.生成id
        //String id = UUID.randomUUID().toString().replace("-","").toUpperCase();

        //2.给company赋值
        company.setId(UtilFuns.generateId());
        //3.调用dao保存
        int row = companyDao.save(company);

        return row > 0;
    }

    /**
     * 跟新用户
     *
     * @param company
     * @return
     */
    @Override
    public boolean update(Company company) {
        int row = companyDao.update(company);
        return row > 0;
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(String id) {
        int row = companyDao.delete(id);
        return row > 0;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return PageResult
     */
    @Override
    public PageResult findPage(int page, int size) {

        int count = companyDao.findCount();
        List<Company> companyList = companyDao.findPage((page - 1) * size, size);

        return new PageResult(count, companyList, page, size);
    }

    /**
     * 使用pageHelp实现分页查询
     *
     * @param page
     * @param size
     * @return PageInfo
     */
    @Override
    public PageInfo findByPageInfo(int page, int size) {
        //使用PageHelper类中提供的静态方法startPage
        PageHelper.startPage(page, size);
        //查询所有数据
        List<Company> companyList = companyDao.findAll();
        //创建返回值对象
        PageInfo pageInfo = new PageInfo(companyList);
        //返回
        return pageInfo;
    }


}
